<?php

class worryComponents extends sfComponents {
    public function executeAnswer_list( sfWebRequest $request ) {
        $worry = $this->getVar('worry');
        $this->worry_answer = Doctrine::getTable('WorryAnswer')->createQuery()->where('worry_id = ?',$worry->getId())->andWhere('parent_id = 0')->orderBy('created_at asc')->execute();
    }
    
    public function executeMy_worry_list(sfWebRequest $request ){
        $this->worrys= Doctrine::getTable('Worry')->createQuery()->where('user_id = ?',$this->getUser()->getAttribute('user_id',0))->limit(8)->execute();
    }
    
    public function executeComment_list(sfWebRequest $request){
        $worry = $this->getVar('worry');
        $dql = Doctrine::getTable('WorryComment')->createQuery()->where('worry_id = ?',$worry->getId())->orderBy('created_at desc');
        $page = $request->getParameter('page',1);
        $this->pager = new sfDoctrinePager('WorryComment',20);
        $this->pager->setQuery($dql);
        $this->pager->setPage($page);
        $this->pager->init();
    }
    
    public function executeHot_tags(sfWebRequest $request){
        $this->tags = Doctrine::getTable('WorryTag')->createQuery()->orderBy('num desc')->limit(12)->execute();
    }
    
    public function executeDefault_no_answer_list(sfWebRequest $request){
        $worry_ids = Doctrine::getTable('WorryStatistical')->createQuery()->where('answer_num = 0')->execute()->toKeyValueArray('worry_id', 'worry_id');
        $this->worrys = Doctrine::getTable('Worry')->createQuery()->whereIn('id',$worry_ids)->andWhere('display = 1')->orderBy('created_at desc')->limit(8)->execute();
    }
    
    //最新已经解决的烦恼列表
    public function executeDefault_worry_list(sfWebRequest $request){
        $worry_ids = Doctrine::getTable('WorryStatistical')->createQuery()->where('answer_num > 0')->execute()->toKeyValueArray('worry_id', 'worry_id');
        $dql = Doctrine::getTable('Worry')->createQuery()->whereIn('id',$worry_ids)->andWhere('display = 1');
        $page = $request->getParameter('page',1);
        $this->pager = new sfDoctrinePager('Worry',20);
        $this->pager->setQuery($dql);
        $this->pager->setPage($page);
        $this->pager->init();
    }
    
    public function executeRelevance(sfWebRequest $request){
        $worry = $this->getVar('worry');
        $tags = $worry->getTagsArray();
        $tag_ids = array();
        
        foreach($tags as $tag_name){
            $tag = Doctrine::getTable('WorryTag')->findOneByName($tag_name);
            if(!$tag)                continue;
            $tag_ids[] = $tag->getId();
        }
        
        $worry_and_tags = Doctrine::getTable('WorryAndTag')->createQuery()->where('worry_id <> ?',$worry->getId())->andWhereIn('worry_tag_id',$tag_ids)->groupBy('worry_id')->execute();
        
        if($worry_and_tags->count() == 0){
            $this->worrys = Doctrine::getTable('Worry')->createQuery()->where('id = 0')->orderBy('created_at desc')->execute();
        }else{
            $worry_ids = $worry_and_tags->toKeyValueArray('worry_id', 'worry_id');
            $this->worrys = Doctrine::getTable('Worry')->createQuery()->whereIn('id',$worry_ids)->andWhere('display = 1')->orderBy('created_at desc')->limit(10)->execute();
        }
    }
}
?>
